package com.njcb.game.web.controller;

import com.njcb.ams.pojo.dto.standard.EntityResponse;
import com.njcb.ams.pojo.dto.standard.PageResponse;
import com.njcb.ams.util.AmsAssert;
import com.njcb.game.application.BusinessGameManager;
import com.njcb.game.pojo.GameResultDetail;
import com.njcb.game.pojo.RegisterInput;
import com.njcb.game.pojo.RegisterOutput;
import com.njcb.game.repository.dao.GameRegisterDAO;
import com.njcb.game.repository.dao.GameResultDAO;
import com.njcb.game.repository.entity.GameRegister;
import com.njcb.game.repository.entity.GameResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户操作类
 *
 * @author LOONG
 */
@RestController
@RequestMapping
@Api(value = "register", tags = "register")
public class RegisterController {

    @Autowired
    private BusinessGameManager businessManager;
    @Autowired
    private GameResultDAO gameResultDAO;
    @Autowired
    private GameRegisterDAO registerDAO;

    @ApiOperation(value = "注册", notes = "注册参赛人信息")
    @RequestMapping(value = "register", method = RequestMethod.POST)
    @ResponseBody
    public EntityResponse<RegisterOutput> register(@RequestBody RegisterInput input) {
        AmsAssert.notNull(input,"注册信息不能为空");
        AmsAssert.notNull(input.getRunnersNo(),"注册信息不能为空");
        AmsAssert.notNull(input.getRunnersName(),"注册信息不能为空");
        AmsAssert.notNull(input.getServerAddr(),"注册信息不能为空");
        String token = businessManager.register(input);
        RegisterOutput registerOutput = new RegisterOutput();
        registerOutput.setToken(token);
        return EntityResponse.build(registerOutput);
    }

    @ApiOperation(value = "验证结果查询", notes = "验证结果查询")
    @RequestMapping(value = "result/{token}")
    @ResponseBody
    public PageResponse<GameResultDetail> result(@PathVariable("token") String token) {
        AmsAssert.notNull(token,"[token]不能为空");
        GameRegister gameRegister = registerDAO.selectByToken(token);
        AmsAssert.notNull(gameRegister,"凭证["+token+"]不存在");
        List<GameResult> results = gameResultDAO.getMapper().selectDetailsByToken(token);
        List<GameResultDetail> resultDetails = new ArrayList<>();
        results.forEach(result ->{
            GameResultDetail resultDetail = new GameResultDetail();
            resultDetail.setRunnersNo(result.getRunnersNo());
            resultDetail.setRunnersName(result.getRunnersName());
            resultDetail.setQuesNo(result.getQuesNo());
            resultDetail.setQuesTitle(result.getQuesTitle());
            resultDetail.setScore(result.getScore());
            resultDetail.setCheckResult("验证通过");
            resultDetail.setFailReason("未知");
            resultDetails.add(resultDetail);
        });
        PageResponse pageResponse = PageResponse.build(resultDetails,resultDetails.size());
        pageResponse.putExtField("token",token);
        return pageResponse;
    }

}
